package panda.natalia.gothread.suspend.instrument;

import java.io.IOError;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;
import java.util.Enumeration;

/**
 * a simple framework of java fiber
 *
 * @Author: scpanda2007
 * @Date: 2019/11/29 11:38
 */
public class ClassLoaderHelper {
    public static InputStream getResourceAsStream(ClassLoader cl, String resouce) throws IOException {
        URL url = getResource(cl, resouce);
        if(url == null) return null;

        URLConnection uc = url.openConnection();
        uc.setUseCaches(false);
        return uc.getInputStream();
    }

    public static URL getResource(ClassLoader cl, String resouce) {
        return cl != null ? cl.getResource(resouce) : ClassLoader.getSystemResource(resouce);
    }

    public static Enumeration<URL> getResources(ClassLoader classLoader, String resources) throws IOException {
        return classLoader != null ? classLoader.getResources(resources) : ClassLoader.getSystemResources(resources);
    }
}
